package com.ebring.shareEducation.controller;

import com.ebring.shareEducation.bean.UserInfo;
import com.ebring.shareEducation.service.EmailService;
import com.ebring.shareEducation.service.UserInfoService;
import com.ebring.shareEducation.utils.Md5Utils;
import com.ebring.shareEducation.utils.RspMsg;
import com.ebring.shareEducation.utils.SendSMS;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@RestController
@CrossOrigin(origins = "http://wangliang.vipgz1.idcfengye.com", maxAge = 3600)
@Api(value = "用户注册登录" ,tags = {"用户注册登录接口"})
public class UserInfoController {


    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private StringRedisTemplate template;

    @Autowired
    private EmailService emailService;
















    /**
     * 通过id查找用户
     * @return
     */
    @GetMapping("/userinfo/findUserByuserIds")
    @ApiOperation(value="通过id查找用户", notes="通过id查找用户")
    @ApiImplicitParams({

            @ApiImplicitParam(paramType="query",name = "userId", value = "用户id", required = true, dataType = "String"),
    })
    public RspMsg<UserInfo> findUserinfoByuserId(String userId) {
        return userInfoService.selectUserByUserId(userId);


    }


    /**
     * 用户注册
     *
     * @param
     * @return
     */

    @PostMapping("/user/register")
    @ApiOperation(value="用户注册", notes="发送短信并返回前台加密过后的codeid")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "yzm", value = "验证码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "codeId", value = "验证码加密", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "email", value = "邮箱", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "password", value = "密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "nation", value = "国籍", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "peincode", value = "邀请码", required = true, dataType = "String"),

    })    public RspMsg add(String yzm, String codeId,String email,String password,String  nation,String peincode) {
        return userInfoService.register(yzm, codeId,email,password,nation,peincode);
    }
    /**
     * 更新用户
     *
     * @param
     * @return 更新成功的结果
     */
    @PostMapping("/userinfo/update")
    @ApiOperation(value="更新用户", notes="根据UserInfo对象更新用户")
    @ApiImplicitParam(paramType="query",name = "UserInfo", value = "用户详细实体user", required = true, dataType = "UserInfo")
    public RspMsg<UserInfo> updateUserinfo(UserInfo userInfo) {

        return userInfoService.updateUser(userInfo);
    }





    /**
     * 实名认证填写身份证信息
     *
     * @param
     */
    @PostMapping("/userinfo/insertCert")
    @ApiOperation(value="实名认证审核通过", notes="实名认证审核通过")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "userid", value = "用户id", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "realname", value = "真实姓名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "certnumber", value = "手机号码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "image1", value = "正面照片路径", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "image2", value = "反面照片路径", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "image3", value = "手持照片路径", required = true, dataType = "String"),
    })    public RspMsg<UserInfo> insertCert(String userid,String realname,String certnumber,String image1,String image2,String image3) {

        return userInfoService.insertCert(userid,realname,certnumber,image1,image2,image3);
    }

    /**
     * 设置支付密码
     *
     * @param
     * @return
     */
    @PostMapping("/userinfo/insertpay")
    @ApiOperation(value="设置支付密码", notes="设置支付密码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "userinfoId", value = "用户编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "payPassword", value = "旧密码", required = true, dataType = "String"),
    })
    public RspMsg insertpayPassword(String userinfoId,  String payPassword) {
        return userInfoService.addpayPasswordInfo(userinfoId, payPassword);
    }

    /**
     * 修改支付密码
     *
     * @param
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @PostMapping("/userinfo/payPassword")
    @ApiOperation(value="修改支付密码", notes="修改支付密码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "userinfoId", value = "用户编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "oldPassword", value = "旧密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "newPassword", value = "新密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "token", value = "登陆后页面token", required = true, dataType = "String"),

    })
    public RspMsg updatepayPassword(String userinfoId, String oldPassword, String newPassword,String token) {
        return userInfoService.changepayPasswordInfo(userinfoId, oldPassword, newPassword,token);
    }

    /**
     * 修改登录密码
     *
     * @param
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @PostMapping("/userinfo/updatePassword")
    @ApiOperation(value="修改web登录密码", notes="修改web登录密码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "userinfoId", value = "用户编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "oldPassword", value = "旧密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "newPassword", value = "新密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "token", value = "登陆后页面token", required = true, dataType = "String"),

    })
    public RspMsg updatePassword(String userinfoId, String oldPassword, String newPassword,String token) {
        return userInfoService.changePasswordInfo(userinfoId, oldPassword, newPassword,token);
    }


    /**
     * 发送邮件
     *
     * @throws IOException
     * @throws
     */
    @PostMapping("/userinfo/sendMsg")
    @ApiOperation(value="发送邮件并返回前台加密过后的codeid", notes="发送邮件")
    @ApiImplicitParam(paramType="query",name = "email", value = "邮箱号码", required = true, dataType = "String")
    @ResponseBody
    public void sendSMS(String email) {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String yzm = SendSMS.getSjs();
        int result = 0;
        try {
            result = emailService.sendSimpleMail(email,"虚拟币交易系统注册验证","您好：\n" +
                    "【bibi】注册验证码："+yzm+"您正在尝试【注册】，该验证码将于30分钟后失效。\n" +
                    "验证码仅用于bibi官网，如果非本人操作，请不要在任何地方输入该验证码并立即修改登录密码！ \n" +
                    "为防止非法诈骗，请勿将帐号、密码、验证码在除官网外任何其他第三方网站输入。\n" +
                    "\n" +
                    "bibi团队\n" +
                    "www.bibi.com");
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject json = new JSONObject();
        if (result > 0) {
            String id = "%$#58412365" + yzm;
            id = Md5Utils.GetMD5Code(id);
            json.put("ifSuccess", true);
            json.put("codeId", id);
        } else {
            json.put("ifSuccess", false);
        }
        out.println(json);
        out.flush();
        out.close();
    }

    /**
     * 邮箱登录
     *
     * @param email
     * @param password
     * @param codeId
     * @return
     */
    @PostMapping("/userinfo/login1")
    @ApiOperation(value="邮箱登录", notes="邮箱登录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "email", value = "邮箱", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "password", value = "验证码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "codeId", value = "获取发送验证码时候的传到前台的codeid", required = true, dataType = "String"),
    })
    public RspMsg tellLogin(String email, String password, String codeId) {

        return userInfoService.loginInfo(email, password, codeId);
    }
}


